package org.jc.subject.domain.service.impl;

import com.alibaba.fastjson.JSON;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.jc.subject.common.enums.IsDeletedFlagEnum;
import org.jc.subject.domain.convert.SubjectCategoryConverter;
import org.jc.subject.domain.convert.SubjectLabelConverter;
import org.jc.subject.domain.entity.SubjectCategoryBO;
import org.jc.subject.domain.entity.SubjectLabelBO;
import org.jc.subject.domain.service.SubjectLabelDomainService;
import org.jc.subject.infra.basic.entity.SubjectCategory;
import org.jc.subject.infra.basic.entity.SubjectLabel;
import org.jc.subject.infra.basic.entity.SubjectMapping;
import org.jc.subject.infra.basic.service.SubjectLabelService;
import org.jc.subject.infra.basic.service.SubjectMappingService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 功能描述
 *
 * @author wanghongqian
 * @date 2024/06/21 12:10
 */
@Service
@Slf4j
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {
    @Resource
    private SubjectLabelService subjectLabelService;

    @Resource
    private SubjectMappingService subjectMappingService;

    @Override
    public void add(SubjectLabelBO subjectLabelBO) {
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBOToLabel(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        subjectLabelService.insert(subjectLabel);
    }

    @Override
    public Boolean update(SubjectLabelBO subjectLabelBO) {
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE
                .convertBOToLabel(subjectLabelBO);
        int count = subjectLabelService.update(subjectLabel);
        return count > 0;
    }

    @Override
    public Boolean delete(SubjectLabelBO subjectLabelBO) {
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE
                .convertBOToLabel(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
        int count = subjectLabelService.update(subjectLabel);
        return count > 0;
    }

    @Override
    public List<SubjectLabelBO> queryLabelByCategoryId(SubjectLabelBO subjectLabelBO){
        if(log.isInfoEnabled()){
            log.info("SubjectLabelDomainServiceImpl.queryLabelByCategoryId.input.{}",JSON.toJSONString(subjectLabelBO));
        }
        Long categoryId = subjectLabelBO.getCategoryId();
        SubjectMapping subjectMapping = new SubjectMapping();
        subjectMapping.setCategoryId(categoryId);
        subjectMapping.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        List<Long> labelIds = subjectMappingService.queryLabelId(subjectMapping);

        List<SubjectLabel> subjectLabels = subjectLabelService.queryByIds(labelIds);
        List<SubjectLabelBO> subjectLabelBOS = SubjectLabelConverter.INSTANCE.convertLabelsToBos(subjectLabels, categoryId);
        if(log.isInfoEnabled()){
            log.info("SubjectLabelDomainServiceImpl.queryLabelByCategoryId.output.{}", JSON.toJSONString(subjectLabelBOS));
        }
        return subjectLabelBOS;
    }


}
